<template>
  <div>
    <div class="search-content">
      <Col class="panel content-header" :span="24">
      <div class="title-bar cf">
        <h5 class="fl">
          <span>
            <a href="javascript:void(0);" class="link" @click="backToLast">
              <i class="fa fa-angle-left" aria-hidden="true"></i>返回上一页</a>
            <span class="dashboard-card-sep">|</span>
            <span class="dashboard-card-name"><span>周电流详情</span></span>
          </span>
        </h5>
      </div>
      </Col>
    </div>
    <div>
      <Row>
        <Col span="24" style="padding:10px;box-sizing:border-box;">
        <DatePicker style="width:240px;" type="daterange" :options="options" class="time-picker" v-model="timeDate" @on-change="onTimeChange" placement="bottom-end" placeholder="请选择日期..." show-week-numbers></DatePicker>
        </Col>
        <!-- 折线图 -->
        <Col span="24" class="panel map-panel" style="margin-bottom:15px;">
        <div style="width:100%;height:350px;" class="blank-chart">
          <div ref="chart" style="width: 100%;height: 100%;"></div>
        </div>
        </Col>
        <!-- 表格 -->
        <Col class="panel table-panel" :span="24" style="overflow-x: auto;margin-bottom: 20px;">
        <AnalTable v-if="tableData.length !=0 " :tableData="tableData" :tableStyle="{ width:'100%' }"></AnalTable>
        </Col>
      </Row>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex"
import AnalTable from "../AnalTable"
import {
  getDeviceListOfNew,
  getNxCurrentDetail,
  getDeviceDetail,
  getIdByDeviceNo
} from "@/api"
import mixin from "./mixin"
import utils from "@/utils/utils"

const { isBlankArray } = utils
export default {
  mixins: [mixin],
  computed: { ...mapGetters(["color"]) },
  mounted() {
    this.initState()
  },
  components: { AnalTable },
  data: () => ({
    tableData: [],
    echartInstence: null
  }),
  methods: {
    onTimeChange() {
      const vm = this
      if (Array.isArray(vm.timeDate)) {
        const value = vm.timeDate
        if (isBlankArray(value)) {
          vm.timeDate = [
            vm
              .moment()
              .add(-6, "days")
              .format(),
            vm.moment().format()
          ]
        }
        const timeStart = vm.moment(vm.timeDate[0]),
          timeEnd = vm.moment(vm.timeDate[1]),
          days = timeEnd.diff(timeStart, "days")
        if (days === 7) {
          vm.initState()
        } else {
          vm.$Message.error("只能选择7天")
        }
      }
    },

    momentFormat(timesup) {
      return this.moment(timesup).format("YYYY-MM-DD HH:mm:ss")
    },

    initState() {
      const vm = this,
        { id, name } = vm.$route.params,
        timeDate = vm.timeDate,
        date = [
          ...vm.timeDate.map(t => vm.moment(t).format("YYYY-MM-DD"))
        ].join("~"),
        params = {
          deviceNo: id,
          date
        }
      vm.$nextTick(() => {
        const dom = vm.$refs.chart
        vm.echartInstence = vm.echarts.init(dom)
        vm.echartInstence.showLoading({ text: "", color: "red" })
        getNxCurrentDetail(params).then(res => {
          const { data } = res
          if (res.code === 200 && JSON.stringify(data) !== "{}") {
            vm.printMap(data, name, date)
            try {
              vm.tableData = [
                {
                  key: "A相最大电流",
                  value: `${data.AEMinVal}(A) (${vm.momentFormat(
                    data.AEMaxDate
                  )})`
                },
                {
                  key: "A相最小电流",
                  value: `${data.AEMinVal}(A) (${vm.momentFormat(
                    data.AEMinDate
                  )})`
                },
                {
                  key: "B相最大电流",
                  value: `${data.BEMaxVal}(A) (${vm.momentFormat(
                    data.BEMaxDate
                  )})`
                },
                {
                  key: "B相最小电流",
                  value: `${data.BEMinVal}(A) (${vm.momentFormat(
                    data.BEMinDate
                  )})`
                },
                {
                  key: "C相最大电流",
                  value: `${data.CEMaxVal}(A) (${vm.momentFormat(
                    data.CEMaxDate
                  )})`
                },
                {
                  key: "C相最小电流",
                  value: `${data.CEMinVal}(A) (${vm.momentFormat(
                    data.CEMinDate
                  )})`
                },
                {
                  key: "零线最大电流",
                  value: `${data.ZeroEMaxVal}(A) (${vm.momentFormat(
                    data.ZeroEMaxDate
                  )})`
                },
                {
                  key: "零线最小电流",
                  value: `${data.ZeroEMinVal}(A) (${vm.momentFormat(
                    data.ZeroEMinDate
                  )})`
                }
              ]
            } catch (e) {
              return false
            }
          } else {
            vm.$Message.error("暂无数据")
            vm.echartInstence.setOption({
              title: {
                text: "暂无数据",
                textStyle: {
                  fontSize: 28,
                  fontWeight: "normal"
                },
                left: "center",
                top: "center"
              }
            })
            vm.echartInstence.hideLoading()
            vm.tableData = [
              {
                key: "A相最大电流",
                value: `-`
              },
              {
                key: "A相最小电流",
                value: `-`
              },
              {
                key: "B相最大电流",
                value: `-`
              },
              {
                key: "B相最小电流",
                value: `-`
              },
              {
                key: "C相最大电流",
                value: `-`
              },
              {
                key: "C相最小电流",
                value: `-`
              },
              {
                key: "零线最大电流",
                value: `-`
              },
              {
                key: "零线最小电流",
                value: `-`
              }
            ]
          }
        })
      })
    },

    printMap(data, name, date) {
      const vm = this,
        { CEList, BEList, AEList, ZeroEList } = data,
        A = AEList.map(t => t.value),
        B = BEList.map(t => t.value),
        C = CEList.map(t => t.value),
        Zero = ZeroEList.map(t => t.value),
        xRay = CEList.map(t =>
          vm.moment(t.createdAt).format("YYYY-MM-DD HH:mm:ss")
        ),
        option = {
          title: {
            text: name,
            left: "center",
            subtext: date
          },
          color: [...this.color],
          tooltip: {
            trigger: "axis",
            axisPointer: {
              type: "cross",
              label: {
                backgroundColor: "#6a7985"
              }
            }
          },
          legend: {
            bottom: 0
          },
          grid: {
            top: "20%",
            left: "4%",
            right: "2%",
            bottom: "50"
          },
          xAxis: {
            type: "category",
            boundaryGap: false,
            data: xRay
          },
          yAxis: {
            name: "单位(A)",
            type: "value"
          },
          series: [
            {
              type: "line",
              name: "A相",
              data: A
            },
            {
              type: "line",
              name: "B相",
              data: B
            },
            {
              type: "line",
              name: "C相",
              data: C
            },
            {
              type: "line",
              name: "零线",
              data: Zero
            }
          ]
        }
      vm.echartInstence.setOption(option)
      vm.echartInstence.hideLoading()
    },

    backToLast() {
      this.$router.go(-1)
    }
  }
}
</script>
